package problems.knapsack;

import ec.BitSolution;
import ec.ECFitnessEvaluator;
import ec.Solution;

public class KnapsackFitnessEvaluator extends ECFitnessEvaluator {

	private KnapsackCreator _knapsack;

	public KnapsackFitnessEvaluator(KnapsackCreator creator) {
		_knapsack = creator;
	}

	public double getFitness(Solution raw_solution) {
		BitSolution solution = (BitSolution) raw_solution;
		
		double fitness = 0;
		int remaining_capacity = _knapsack.getCapacity();
		int value = 0;

		/*
		 * figure out the weight "remaining" after packing the knapsack and
		 * value of what we've put in it
		 */
		for (int i = 0; i < solution.getNumBits(); i++) {
			if (solution.getBit(i)) {
				remaining_capacity -= _knapsack.getWeight(i);
				value += _knapsack.getProfit(i);
			}
		}

		if (remaining_capacity >= 0) {
			// We haven't put to much in, so the fitness is simply the value of our goods
			fitness = value;
		} else {
			/*
			 * we are over weight, so the fitness will be negative by the amount
			 * we have gone over weight
			 */
			fitness = remaining_capacity;
		}

		return fitness;
	}
		

	// public boolean compare(double value1, double value2) {
	// // For the knapsack lower is better
	// return (value1 < value2);
	// }
	//	
	// public double getWorstFitness() {
	// // Since lower fitness is better, worst is now positive infinity
	// return Double.POSITIVE_INFINITY;
	//	}

	public double getMaxFitness() {
		return 0;
	}

}
